package cn.wolfcode.luowowo.service.impl;

import cn.wolfcode.luowowo.domain.Destination;
import cn.wolfcode.luowowo.domain.Strategy;
import cn.wolfcode.luowowo.domain.StrategyCatalog;
import cn.wolfcode.luowowo.domain.StrategyTheme;
import cn.wolfcode.luowowo.query.StrategyQuery;
import cn.wolfcode.luowowo.redis.vo.StrategyStatisVO;
import cn.wolfcode.luowowo.repository.StrategyCatalogRepository;
import cn.wolfcode.luowowo.repository.StrategyRepository;
import cn.wolfcode.luowowo.repository.StrategyThemeRepository;
import cn.wolfcode.luowowo.service.IDestinationService;
import cn.wolfcode.luowowo.service.IStrategyCatalogService;
import cn.wolfcode.luowowo.service.IStrategyService;
import cn.wolfcode.luowowo.service.IStrategyThemeService;
import cn.wolfcode.luowowo.util.DBHelper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;
import java.util.Optional;

@Service
public class StrategyServiceImpl implements IStrategyService {

    @Autowired
    private StrategyRepository repository;
    @Autowired
    private IStrategyCatalogService strategyCatalogService;
    @Autowired
    private IStrategyThemeService strategyThemeService;
    @Autowired
    private IDestinationService destinationService;
    @Override
    public Strategy get(String id) {
        Optional<Strategy> optional = repository.findById(id);
        //如果数据库不是空的
        if (optional.isPresent()){
            return optional.get();
        }
        return null;
    }

    @Autowired
    private MongoTemplate template;
    /**
     * 分页操作
     * @param qo 分页条件
     * @return
     */
    @Override
    public Page list(StrategyQuery qo) {
        Query query = new Query();
        //如果有目的地的id的话就进行条件查询  得出明细
        if (StringUtils.hasLength(qo.getDestId())){
            query.addCriteria(Criteria.where("destId").is(qo.getDestId()));
        }
        if (StringUtils.hasLength(qo.getThemeId())){
            query.addCriteria(Criteria.where("themeId").is(qo.getThemeId()));
        }
        Page<Strategy> page = DBHelper.query(template, Strategy.class, query, qo.getPageable());
        return page;
    }

    @Override
    public void save(Strategy strategy) {
        //1.目的地名称和id
        StrategyCatalog strategyCatalog = strategyCatalogService.get(strategy.getCatalogId());
        strategy.setDestId(strategyCatalog.getDestId());
        strategy.setDestName(strategyCatalog.getDestName());
        //2.主题名称
        StrategyTheme strategyTheme = strategyThemeService.get(strategy.getThemeId());
        strategy.setThemeName(strategyTheme.getName());
        //3.分类名称
        strategy.setCatalogName(strategyCatalog.getName());
        //4.是否在国外
        //查询之前写的方法吐司,直接找到目的地的老爸get(0),因为是反转过的
        List<Destination> toasts = destinationService.getToasts(strategyCatalog.getDestId());
        if (toasts != null && toasts.size()>0){
            if ("中国".equals(toasts.get(0).getName())){
                strategy.setIsabroad(Strategy.ABROAD_NO);
            }else {
                strategy.setIsabroad(Strategy.ABROAD_YES);
            }
        }
        if (!StringUtils.hasLength(strategy.getId())){
            strategy.setId(null);
            strategy.setCreateTime(new Date());
            repository.save(strategy);
        }else{
            Query query = new Query();
            query.addCriteria(Criteria.where("id").is(strategy.getId()));
            DBHelper.update(template,query,strategy,"destId","destName","themeId","themeName",
                    "title","subTitle","summary","coverUrl","isabroad","state","content");
        }
    }

    @Override
    public void delete(String id) {
        repository.deleteById(id);
    }

    @Override
    public void changeState(String id, int state) {
        Query query = new Query();
        query.addCriteria(Criteria.where("id").is(id));
        Update update = new Update();
        update.set("state",state);
        template.updateMulti(query,update,Strategy.class);
    }

    @Override
    public List<Strategy> findByDestId(String id) {
        List<Strategy> byDestId = repository.findByDestId(id);
        return byDestId;
    }

    @Override
    public List<Strategy> findByDestIdForTravel(String destId) {
        //查询点击前三的攻略
        PageRequest of = PageRequest.of(0, 3, Sort.Direction.DESC, "viewnum");
        List<Strategy> byDestId = repository.findByDestId(destId,of);
        return byDestId;
    }

    @Override
    public List<Strategy> list() {
        return repository.findAll();
    }

    @Override
    public void updateToDate(StrategyStatisVO vo) {
        Query query = new Query();
        Strategy strategy = new Strategy();
        query.addCriteria(Criteria.where("id").is(vo.getStrategyId()));
        BeanUtils.copyProperties(vo,strategy);
        DBHelper.update(template,query,strategy,"viewnum","replynum","favornum","sharenum","thumbsupnum");
    }

    @Override
    public List<Strategy> getByDestName(String keyword) {
        return repository.findByDestName(keyword);
    }
}
